package com.ecoolex.cms.system.core.manager;

import java.time.Instant;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ecoolex.cms.system.core.dao.VRNaviPathDAO;
import com.ecoolex.cms.system.entity.VRNaviPath;
import com.ecoolex.cms.system.entity.VRNaviPath.Column;
import com.ecoolex.cms.system.entity.VRNaviPathExample;
import com.ecoolex.cms.system.entity.VRScene;

@Service
public class VRNaviPathManager {
	@Autowired
	private VRNaviPathDAO dao;

	@Transactional
	public Boolean update(VRScene scene, List<VRNaviPath> list, String updateUser) {
		deleteBySceneId(scene.getSceneId(), updateUser);

		list.forEach(navi -> {
			navi.setSceneId(scene.getSceneId());
			navi.setDeleted(VRNaviPath.NOT_DELETED);
			navi.setCreateUser(updateUser);
			navi.setCreateTime(Date.from(Instant.now()));
			navi.setUpdateUser(updateUser);
			navi.setUpdateTime(Date.from(Instant.now()));

		});
		dao.batchInsert(list);
		return true;
	}

	public Boolean deleteBySceneId(Integer sceneId, String updateUser) {
		VRNaviPathExample example = new VRNaviPathExample();
		example.createCriteria().andSceneIdEqualTo(sceneId).andDeletedEqualTo(VRNaviPath.NOT_DELETED);

		VRNaviPath record = new VRNaviPath();
		record.setDeleted(VRNaviPath.IS_DELETED);
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByExampleSelective(record, example, Column.deleted, Column.updateUser, Column.updateTime);
		return true;
	}

	public Long countByBySceneId(Integer sceneId) {
		VRNaviPathExample example = new VRNaviPathExample();
		example.createCriteria().andSceneIdEqualTo(sceneId).andDeletedEqualTo(VRNaviPath.NOT_DELETED);
		return dao.countByExample(example);
	}

	public List<VRNaviPath> queryBySceneId(Integer sceneId) {
		VRNaviPathExample example = new VRNaviPathExample();
		example.createCriteria().andSceneIdEqualTo(sceneId).andDeletedEqualTo(VRNaviPath.NOT_DELETED);
		return dao.selectByExample(example);
	}
}